package com.huitone.gddw.controller.worktable.paramconfigmanage;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.ErrorMng;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IErrorMngService;
import com.huitone.gddw.service.dto.ErrorMngDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 错误码管理 控制器
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="errorMng")
public class ErrorMngController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(ErrorMngController.class);
	
	@Autowired
	private IErrorMngService errorMngService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<ErrorMngDTO> query(QueryPage<ErrorMng> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询错误码";
		Map<String, Object> params = new HashMap<String, Object>();
		String errCode = request.getParameter("errCode");
		String errMsg = request.getParameter("errMsg");
		String fromType = request.getParameter("fromType");
		String errType = request.getParameter("errType");
		String status = request.getParameter("status");
		params.put("errCode", errCode);
		params.put("errMsg", errMsg);
		params.put("fromType", fromType);
		params.put("errType", errType);
		params.put("status", status);
		page.setParams(params);
		QueryPage<ErrorMngDTO> result = null;
		try {
			result = errorMngService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询短信类型信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 更改状态
	 * @param recId 记录ID
	 * @param status 更改后状态
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="changeStatus")
	public Message changeStatus(String recId, String status) {
		try {
			ErrorMng entity = errorMngService.selectByPrimaryKey(recId);
			entity.setStatus(status);
			errorMngService.changeStatus(entity);
			return new Message(true, "操作成功！");
		} catch (Exception e) {
			LOGGER.error("更改错误码状态过程中出现异常：" + e.getMessage(), e);
			return new Message(false, "操作失败！");
		}
	}
	
	/**
	 * 修改页面数据准备
	 * @param recId 记录ID
	 * @return ErrorMngDTO
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public ErrorMngDTO toUpdate(String recId) {
		try {
			ErrorMngDTO entity = errorMngService.toUpdate(recId);
			return entity;
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return new ErrorMngDTO();
		}
	}
	
	/**
	 * 保存
	 * @param entity ErrorMng
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(ErrorMng entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加错误码";
		boolean isSuccess = true;
		String msg;
		try {
			errorMngService.save(entity);
			msg = "保存成功！";
		} catch (Exception e) {
			LOGGER.error("保存错误码信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "保存失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 更新
	 * @param entity ErrorMng
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(ErrorMng entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改错误码";
		boolean isSuccess = true;
		String msg;
		try {
			ErrorMng errorMng = errorMngService.selectByPrimaryKey(entity.getRecId());
			errorMng.setFromType(entity.getFromType());
			errorMng.setErrType(entity.getErrType());
			errorMng.setErrCode(entity.getErrCode());
			errorMng.setStatus(entity.getStatus());
			errorMng.setErrMsg(entity.getErrMsg());
			errorMng.setInnerCode(entity.getInnerCode());
			errorMngService.update(errorMng);
			msg = "修改成功！";
		} catch (Exception e) {
			LOGGER.error("修改错误码信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 删除
	 * @param recId 记录ID
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String recId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除错误码";
		boolean isSuccess = true;
		String msg;
		try {
			errorMngService.delete(recId);
			msg = "删除成功！";
		} catch (Exception e) {
			LOGGER.error("删除错误码信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "删除失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
}
